# Reference

These are all the types that GraphQL Shield exposes as an API.

## Types

```ts
// Rule
function rule(name?: string, options?: IRuleOptions): (func: IRuleFunction) => Rule

type IFragment = string
type ICacheOptions = 'strict' | 'contextual' | 'no_cache' | boolean
type IRuleResult = boolean | string | Error

type IRuleFunction = (parent?: any, args?: any, context?: any, info?: GraphQLResolveInfo) => IRuleResult | Promise<IRuleResult>

interface IRuleOptions {
  cache?: ICacheOptions
  fragment?: IFragment
}

// Input
function inputRule(name?: string): (yup: Yup => Yup.Schema, context: any) => Rule

// Logic
function and(...rules: IRule[]): LogicRule
function chain(...rules: IRule[]): LogicRule
function or(...rules: IRule[]): LogicRule
function race(...rules: IRule[]): LogicRule
function not(rule: IRule, error?: string | Error): LogicRule
const allow: LogicRule
const deny: LogicRule

import { GraphQLResolveInfo } from 'graphql'
import { IMiddlewareGenerator } from 'graphql-middleware'

// Rule
export type IFragment = string
export type ICache = 'strict' | 'contextual' | 'no_cache'
export type IRuleResult = boolean | string | Error
export type IRuleFunction = (
  parent?: any,
  args?: any,
  context?: any,
  info?: GraphQLResolveInfo,
) => IRuleResult | Promise<IRuleResult>

// Rule Constructor Options
type ICacheOptions = 'strict' | 'contextual' | 'no_cache' | boolean

interface IRuleOptions {
  cache?: ICacheOptions
  fragment?: IFragment
}

// Rules Definition Tree
export type ShieldRule = IRule | ILogicRule

interface IRuleFieldMap {
  [key: string]: ShieldRule
}

interface IRuleTypeMap {
  [key: string]: ShieldRule | IRuleFieldMap
}

type IRules = ShieldRule | IRuleTypeMap

type IHashFunction = (arg: { parent: any; args: any }) => string

type IFallbackErrorMapperType = (
  err: unknown,
  parent: object,
  args: object,
  ctx: IShieldContext,
  info: GraphQLResolveInfo,
) => Promise<Error> | Error

export type IFallbackErrorType = Error | IFallbackErrorMapperType

// Generator Options
interface IOptions {
  debug?: boolean
  allowExternalErrors?: boolean
  fallbackRule?: ShieldRule
  fallbackError?: string | IFallbackErrorType
  hashFunction?: IHashFunction
}

declare function shield(ruleTree: IRules, options: IOptions): IMiddlewareGenerator
```
